package com.sinodata.bsm.cicp.collector.nginx;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CollectorUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class NginxCollector extends Collector {

    private static final Logger logger = Logger.getLogger(NginxCollector.class.getName());
    private Long[] lastValue = null;
    private long lastTime = -1;

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String url = this.getParamMap().get("URLAddress").trim();
        String[] propMetas = getPropMeta();
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String url1 = url + "/nginx_status";
        Long[] tmpValues = new Long[9];
        String respXml;
        Matcher match;
        try {
            for (int i = 0; i < 3; i++) {
                Long begin = System.currentTimeMillis();
                respXml = CollectorUtil.getHttpXml(url1);
                tmpValues[0] = System.currentTimeMillis() - begin;
                tmpValues[1] = 0L;
                if (tmpValues[0] > 10000) {
                    tmpValues[1] = 3L;
                }
            }
            respXml = CollectorUtil.getHttpXml(url1);
            Pattern patt = java.util.regex.Pattern.compile("Active connections:\\s(\\d+).*");
            System.out.println();
            match = patt.matcher(respXml);
            if (match.find()) {
                tmpValues[2] = Long.parseLong(match.group(1).toString());
            }
            patt = java.util.regex.Pattern.compile("\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+");
            match = patt.matcher(respXml);
            if (match.find()) {
                tmpValues[6] = Long.parseLong(match.group(1).toString());
                tmpValues[7] = Long.parseLong(match.group(2).toString());
                tmpValues[8] = Long.parseLong(match.group(3).toString());

            }
            patt = java.util.regex.Pattern.compile("Reading:\\s+(\\d+)\\s+Writing:\\s+(\\d+)\\s+Waiting:\\s+(\\d+)\\s+");
            match = patt.matcher(respXml);
            if (match.find()) {
                tmpValues[3] = Long.parseLong(match.group(1).toString());
                tmpValues[4] = Long.parseLong(match.group(2).toString());
                tmpValues[5] = Long.parseLong(match.group(3).toString());
            }

            Long currentTimeMillis = System.currentTimeMillis();
            if (lastTime > 0 && lastValue != null && lastValue.length == tmpValues.length && currentTimeMillis > lastTime) {
                ResPropertyValue propertyValueX = new ResPropertyValue();
                ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
                for (int j = 0; j < 6; j++) {
                    propValues[j] = ResPropertyValue.createPropValue(tmpValues[j].toString(), propMetas[j]);
                }
                for (int i = 0; i < 3; i++) {
                    long v = (tmpValues[i + 6] - lastValue[i + 6]) / ((currentTimeMillis - lastTime) / 1000);
                    propValues[i + 6] = ResPropertyValue.createPropValue(String.valueOf(v), propMetas[i + 6]);
                }
                propertyValueX.setValues(propValues);
                values.add(propertyValueX);
            }
            lastTime = currentTimeMillis;
            lastValue = tmpValues;
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }

        return values;
    }

    public String[] getParamMeta() {
        //        return new String[] { "URL地址" };
        return new String[] { "URLAddress" };
    }

    public String[] getPropMeta() {
        //        return new String[] { "响应时间", "响应状态(0:正常1:不可用3:超时)", "活动链接数", "正在读取客户端数", "正在写入客户端数", "驻留客户端数", "连接速率", "处理速率", "请求速率" };
        return new String[] { "ResponseTime", "ResponseStatus", "ActiveLinkNum", "BeingReadingClientsNum", "BeingWritingClientsNum", "ResidentClientsNum", "ConnSpeed", "HandleSpeed", "RequestSpeed" };
    }

    public static void main(String[] args) {
        //        ApacheStatusCollector a = new ApacheStatusCollector();
        //        a.collect("");
    }

}
